/** 
 * !\file   CompressorProcess.h
 *  \brief  Code exécuté par le porcessus instrumenteur
 */

#ifndef COMPRESSORPROCESS_H
#define	COMPRESSORPROCESS_H


#include <queue>
#include <cstdio>
#include <pthread.h>
#include <list>

#include "../stack_trace/stack_trace.h"
#include "alloc_event_t.h"


using namespace std;

/**
 * \brief
 */
typedef struct {
    
    /**
     * \brief
     */
    void * content;
    
    /**
     * \brief
     */
    long  events_count;
}trace_part;


class CompressorProcess {
    
    
private:
    
    /**
     * \brief un pointeur vers la shared memory
     */
    char* shmem_events;
#ifndef WITH_PTHREADS
    char* shmem_stacktraces;
    list<callers_tree*> stack_traces_demangled;
#endif
    unsigned long* shmem_events_count;
    
    /**
     * \brief
     */
    FILE* stack_traces_pipe;
    
    /**
     * \brief la zone mémoire qui contiendera la trace en entier
     */
    char* trace_inmem;
    
    /**
     * \brief
     */
    size_t trace_size;
    
    /**
     * \brief
     */
    size_t stacktrace_size;
    
    /**
     * \brief 
     */
    pthread_t btrace_reader;

    
public:
    /**
     * \brief
     */
    CompressorProcess();
    /**
     * \brief
     * @param orig
     */
    CompressorProcess(const CompressorProcess& orig);
    
    
    /**
     * \brief
     */
    virtual ~CompressorProcess();
    
    
private:
    
    /**
     * \brief copie le contenu de la shared memory dans son espace d'adressage
     */
    void copyToLocalMemoryShmemContent(long count);
    
public:
    /**
     * \brief initalise la communication inter-processus
     */
    void initIPC();
    
    /**
     * \brief finalise la communication inter-processus
     */
    void finitIPC();
    
    
    /**
     * \brief
     */
    
    alloc_event_t* operator [](size_t i);
    
    /**
     * \brief
     */
    void startWork();
    
    /**
     * \brief
     */
    void writeMemoryTraceToFile();
    
    
    /**
     * \brief
     */
    void copyToLocalMemoryStackTraces(long count);
    
    /**
     * \brief
     */
    void writeBacktracesToFile(bool yes_or_no);
};

#endif	/* COMPRESSORPROCESS_H */

